package com.zfy.cmsservice.controller;


import com.zfy.cmsservice.entity.Teachers;
import com.zfy.cmsservice.entity.query.TeacherQuery;
import com.zfy.cmsservice.service.TeachersService;
import com.zfy.commonutils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 教师表 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2023-11-05
 */
@RestController
@RequestMapping("/cmsservice/teachers")
@Api(description = "教师管理-教师信息维护")
public class TeachersController {
    @Autowired
    private TeachersService teachersService;

    // 分页查询教师 - 支持根据名称,性别,添加时间检索
    @ApiOperation(value = "分页查询教师")
    @PostMapping("pageTeacher/{page}/{size}")
    public R pageTeacher(@PathVariable long page, @PathVariable long size,
                         @RequestBody(required = false) TeacherQuery teacherQuery){
            return teachersService.pageTeacherCondition(page,size,teacherQuery);
    }

    // 添加教师
    @ApiOperation(value = "添加教师")
    @PostMapping("addTeacher")
    public R addTeacher(@RequestBody Teachers teachers){
        boolean save = teachersService.save(teachers);
        if(save) {
            return R.ok().message("添加成功");
        }
        return R.error().message("添加失败");
    }

    // 修改教师信息
    @ApiOperation(value = "修改教师信息")
    @PostMapping("updateTeacher")
    public R updateTeacher(@RequestBody Teachers teachers){
        if (StringUtils.isEmpty(teachers)) {
            // 传参错误异常
            return R.httpError();
        }
        boolean update = teachersService.updateById(teachers);
        if(update) {
            return R.ok().message("修改成功");
        }
        return R.error().message("修改失败");
    }

    // 逻辑删除教师
    @ApiOperation(value = "删除教师")
    @PostMapping("removeTeacher/{id}")
    public R removeTeacher(@PathVariable String id){
        boolean remove = teachersService.removeById(id);
        if(remove) {
            return R.ok().message("删除成功");
        }
        return R.error().message("删除失败");
    }

    //根据id查询教师
    @ApiOperation(value = "根据id查询教师")
    @GetMapping("getTeacher/{id}")
    public R getTeacherById(@PathVariable String id) {
        Teachers teacher = teachersService.getById(id);
        return R.ok().data("item", teacher);
    }

    //查询所有讲师
    @ApiOperation(value = "查询所有讲师")
    @GetMapping("findAllTeacher")
    public R findAllTeacher() {
        List<Teachers> content = teachersService.findAllTeacher();
        return R.ok().data("content", content);
    }


}

